package com.getinstaliker.screen;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.getinstaliker.activity.MainFragmentActivity;
import com.getinstaliker.httptask.GetInstalikerTask;
import com.getinstaliker.httptask.model.ResultModel;
import com.getinstaliker.instagram.InstagramBaseTask.InstagramTaskListener;
import com.getinstaliker.instagram.SessionStore;
import com.getinstaliker.instagram.model.MediaImageModel.ImageData;
import com.getinstaliker.instagram.model.MediaImageModel.ImageInfo;
import com.getinstaliker.instagram.model.MediaImageModel.MediaData;
import com.getinstaliker.screen.common.BaseFragmentScreen;
import com.getinstaliker.screen.common.BaseGetInstalikerHeaderFragment;
import com.getinstaliker.util.DialogUtil;
import com.getinstaliker.util.InstalikerCoinsController;
import com.getinstaliker.util.DialogUtil.DialogListener;
import com.getinstaliker.util.InstalikerDialogInfo;
import com.getinstaliker.util.UniversalImageLoader;
import com.instagram.getinstaliker.R;


// TODO: Auto-generated Javadoc
/**
 * The Class SpendFragment.
 */
@SuppressLint("ValidFragment")
public class SpendFragment extends BaseGetInstalikerHeaderFragment implements OnClickListener {

	/** The m seek bar. */
	private SeekBar mSeekBar;
	
	/** The m spend coins. */
	private Button mBtnSpendCoins;
	
	/** The m enter num likes. */
	private Button mBtnEnterNumLikes;
	
	/** The m img photo. */
	private ImageView mImgPhoto;
	
	/** The m current data. */
	private MediaData mCurrentData;
	
	/** The m tv info. */
	private TextView mTvInfo;
	/**
	 * Instantiates a new spend fragment.
	 *
	 * @param data the data
	 */
	public SpendFragment(MediaData data) {
		mCurrentData = data;
	}
	
	/* (non-Javadoc)
	 * @see com.getinstaliker.screen.common.BaseFragmentScreen#onCreateContentView(android.view.LayoutInflater, android.view.ViewGroup)
	 */
	@Override
	protected View onCreateContentView(LayoutInflater inflater,
			ViewGroup container) {
		View vContent 		= inflater.inflate(R.layout.spend_screen, container, false);
		mSeekBar 			= (SeekBar) vContent.findViewById(R.id.seekBar);
		mImgPhoto			= (ImageView) vContent.findViewById(R.id.imgUserPhoto);
		mBtnSpendCoins 		= (Button) vContent.findViewById(R.id.btnSpendCoins);
		mBtnEnterNumLikes 	= (Button) vContent.findViewById(R.id.btnEnterSpecificNumber);
		mTvInfo				= (TextView) vContent.findViewById(R.id.tvInfo);
		mBtnSpendCoins.setOnClickListener(this);
		mBtnEnterNumLikes.setOnClickListener(this);
		vContent.findViewById(R.id.imgWraper).setLayoutParams(new LinearLayout.LayoutParams
												(LayoutParams.MATCH_PARENT, MainFragmentActivity.SCREEN_WIDTH));
		init();
		return vContent;
	}
	
	/**
	 * Inits the.
	 */
	private void init(){
		mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
				updateProcess(progress);
			}
		});
		if(mCurrentData!=null){
			loadData(mCurrentData);
		}
	}
	
	/* (non-Javadoc)
	 * @see com.getinstaliker.screen.common.BaseGetInstalikerHeaderFragment#updateCoins()
	 */
	@Override
	public void updateCoins() {
		super.updateCoins();
		resetSeekBar();
	}
	
	/**
	 * Reset seek bar.
	 */
	public void resetSeekBar(){
		String coins = SessionStore.getInstance().getUserCoins();
		if(coins!=null){
			mSeekBar.setMax(Integer.valueOf(coins));
			if(mSeekBar.getMax()>0){
				mSeekBar.setProgress(mSeekBar.getMax());
			}
		}
	}
	
	/**
	 * Update process.
	 *
	 * @param progress the progress
	 */
	public void updateProcess(int progress){
		if(progress%2==0){
			mBtnSpendCoins.setText("Spend "+progress+" coins");
			mTvInfo.setText("you'll get "+progress/2 +" likes for "+progress+" coins");
			mBtnSpendCoins.setTag(progress);
		}else{
			mBtnSpendCoins.setText("Spend "+(progress-1)+" coins");
			mTvInfo.setText("you'll get "+progress/2 +" likes for "+(progress-1)+" coins");
			mBtnSpendCoins.setTag(progress-1);
		}
	}
	
	/**
	 * Show input dialog.
	 */
	public void showInputDialog(){
		DialogUtil.showInputDialog(getActivity(), InstalikerDialogInfo.TITLE_NUMBER_OF_LIKE, InstalikerDialogInfo.MESSAGE_NUMBER_OF_LIKE, new DialogListener() {
			
			@Override
			public void onCancel() {}
			
			@Override
			public void onAccept(String result) {
				int progress = Integer.valueOf(result);
				if(progress<=mSeekBar.getMax()){
					mSeekBar.setProgress(progress);
				}else{
					mSeekBar.setProgress(mSeekBar.getMax());
					updateProcess(progress);
				}
			}
		});
	}
	
	/**
	 * Buy coins.
	 */
	public void buyCoins(){
		Toast.makeText(getActivity(), "Buy Coins", Toast.LENGTH_SHORT).show();
	}
	
	
	/**
	 * Show spend dialog.
	 */
	public void showSpendDialog(){
		int spends = (Integer) mBtnSpendCoins.getTag();
		if(spends<=mSeekBar.getMax()){
			String message = "Are you sure spends "+spends+" coins for "+spends/2+ " likes?";
			DialogUtil.showDialog(getActivity(), "Message", message, new DialogListener() {
				
				@Override
				public void onCancel() {}
				
				@Override
				public void onAccept(String result) {
					spendTask();
				}
			});
		}else{
			DialogUtil.showDialog(getActivity(), InstalikerDialogInfo.TITLE_WARNING, InstalikerDialogInfo.MESSAGE_NOT_ENOUGH_COINT, new DialogListener() {
				
				@Override
				public void onCancel() {}
				
				@Override
				public void onAccept(String result) {
					buyCoins();
				}
			});
		}
	}
	
	/**
	 * Spend task.
	 */
	public void spendTask(){
		if(mBtnSpendCoins.getTag()==null||mImgPhoto.getTag()==null)
			return;
			int like_needed = (Integer)mBtnSpendCoins.getTag()/2;
			GetInstalikerTask.SpendCoinsTask(getActivity(), SessionStore.getInstance().getInstaAccessToken(), String.valueOf(like_needed), (String)mImgPhoto.getTag(), new InstagramTaskListener<ResultModel>() {
				
				@Override
				public void onTaskComplete(ResultModel ob) {
					if(ob!=null&&ob.status.equals("OK")){
						InstalikerCoinsController.spendCoins((Integer)mBtnSpendCoins.getTag());
						updateCoins();
						((BaseGetInstalikerHeaderFragment)SpendFragment.this.getParentFragment()).updateCoins();
						DialogUtil.showConfirmDialog(getActivity(), InstalikerDialogInfo.TITLE_BUY_LIKE_SUCCESS, InstalikerDialogInfo.MESSAGE_BUY_LIKE_SUCCESS,new DialogListener() {
							
							@Override
							public void onCancel() {}
							
							@Override
							public void onAccept(String result) {
								onBack();
							}
						});
					}
				}
			});
	}
	
	/**
	 * Load data.
	 *
	 * @param data the data
	 */
	public void loadData(MediaData data){
		if(data!=null){
			mImgPhoto.setImageBitmap(null);
			ImageInfo imgInfo = chooseResolution(data.images);
			int width = Integer.valueOf(imgInfo.width);
			int height = Integer.valueOf(imgInfo.height);
			UniversalImageLoader.getInstance().loadImageView(mImgPhoto, imgInfo.url, width, height);
			if(data.id!=null){
				mImgPhoto.setTag(data.id);
			}
		}
	}
	
	/**
	 * Choose resolution.
	 *
	 * @param imageData the image data
	 * @return the image info
	 */
	public ImageInfo chooseResolution(ImageData imageData){
		final int IMAGE_SIZE = MainFragmentActivity.SCREEN_WIDTH;
		if(Integer.valueOf(imageData.standard_resolution.width)<IMAGE_SIZE){
			return imageData.standard_resolution;
		}else if(Integer.valueOf(imageData.low_resolution.width)<IMAGE_SIZE){
			return imageData.low_resolution;
		}else{
			return imageData.thumbnail;
		}
	}
	
	/* (non-Javadoc)
	 * @see com.getinstaliker.screen.common.BaseGetInstalikerHeaderFragment#onBack()
	 */
	@Override
	public void onBack() {
		if(getParentFragment() instanceof BaseFragmentScreen){
			((BaseFragmentScreen)getParentFragment()).hideChildFragment(this);
		}
	}

	/* (non-Javadoc)
	 * @see com.getinstaliker.screen.common.BaseGetInstalikerHeaderFragment#getTitleResId()
	 */
	@Override
	protected int getTitleResId() {
		return R.string.spend;
	}

	/* (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnSpendCoins:
			showSpendDialog();
			break;
		case R.id.btnEnterSpecificNumber:
			showInputDialog();
			break;
		default:
			break;
		}
	}

}
